//
// Created by 12992 on 24-10-14.
//

#ifndef MEDIA_TRANSCODER_MAINWINDOW_H
#define MEDIA_TRANSCODER_MAINWINDOW_H

#include "video_meta.h"
#include "video_config.hpp"
#include "audio_config.hpp"
#include "widgets/audioencodeconfigitem.h"
#include "widgets/videoencodeconfigwidget.h"
#include "widgets/transcoderqueuewidget.h"
#include <QMainWindow>
#include <QProgressBar>
#include <unordered_map>
#include <any>
#include <memory>

namespace gui
{
    namespace widgets
    {
        class FileSelector;

        class VideoPreview;

        class IconButton;
    }

    QT_BEGIN_NAMESPACE

    namespace Ui
    {
        class MainWindow;
    }

    QT_END_NAMESPACE

    class MainWindow : public QMainWindow
    {
        Q_OBJECT

        using preset_map = std::unordered_map<std::string, std::pair<utils::video_config, utils::audio_config>>;
        using preset_ptr = std::unique_ptr<std::pair<utils::video_config, utils::audio_config>>;

    public:
        explicit MainWindow(QWidget* parent = nullptr);

        ~MainWindow() override;

    private slots:
        void cancelSelectFile();

        void openMediaFile(const QString& path);

        void showOpenView();

        void setOutputPath();

        void showToolBar(int currentIndex);

        void startTranscode();

        void addTranscodeQueue();

        void showTranscodeError(std::string errorMsg);

    private:
        void init_widgets();

        void init_signals();

        void init_metadata_widget_labels();

        void set_metadata_widget_labels(const QString& formatInfo,
                                        const QString& formatInfo2,
                                        const QString& mainVideoInfo,
                                        const QString& mainAudioInfo, const QString& sizeInfo);

        void set_file_info_widget_labels(const QString& path);

        void set_video_preview_widget();

        void set_output(const QString& inputPath);

        void set_audio_encode_config_widget(const utils::audio_config& currPreset);

        void init_video_preview_widget();

        void init_video_encode_config_widget();

        void init_transcoder_queue();

        void init_tool_bar();

        void clean_prev_metadata();

        void read_preset();

        void read_setting();

        void read_config();

    private:
        Ui::MainWindow* ui;
        QProgressBar* progressBar{};
        widgets::FileSelector* fileSelector{};
        widgets::VideoPreview* videoPreview{};
        widgets::VideoEncodeConfigWidget* videoEncodeConfig{};
        std::unique_ptr<utils::video_meta> metadata{};
        preset_map preset{};
        preset_ptr currPreset{};
        std::unordered_map<std::string, std::any> setting{};
        widgets::IconButton* openFileButton{};
        widgets::IconButton* startTranscodeButton{};
        widgets::IconButton* pauseTranscodeButton{};
        widgets::IconButton* insertQueueButton{};
        widgets::TranscoderQueueWidget* transcoderQueueWidget{};
    };
} // gui

#endif //MEDIA_TRANSCODER_MAINWINDOW_H
